package com.jfcloud.mall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jfcloud.common.http.RespDataT;
import com.jfcloud.mall.product.domain.JfcloudProduct;
import com.jfcloud.mall.product.domain.JfcloudProductSku;
import com.jfcloud.mall.product.domain.JfcloudProductType;
import com.jfcloud.mall.product.domain.dto.ProductQueryForm;
import com.jfcloud.mall.product.domain.dto.ProductSkuVo;
import com.jfcloud.mall.product.domain.dto.ProductVo;
import com.jfcloud.mall.product.mapper.ProductMapper;
import com.jfcloud.mall.product.persistent.JfcloudProductService;
import com.jfcloud.mall.product.persistent.JfcloudProductSkuService;
import com.jfcloud.mall.product.persistent.JfcloudProductTypeService;
import com.jfcloud.mall.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.List;

@Api(tags = "商品API")
@Controller
@RequestMapping("/product")
public class ProductController {
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);
	
	@Autowired
	private ProductMapper productMapper;

	@Autowired
	private JfcloudProductService jfcloudProductService;

	@Autowired
	private JfcloudProductSkuService jfcloudProductSkuService;

	@Autowired
	private JfcloudProductTypeService jfcloudProductTypeService;

	@Autowired
	private ProductService productService;


	@ApiOperation("商品分类")
	@GetMapping(path = "/types")
	@ResponseBody
	public RespDataT<List<JfcloudProductType>> types() {
		try{
			List<JfcloudProductType> datas = jfcloudProductTypeService.list(
					new QueryWrapper<JfcloudProductType>()
							.eq("status", 1)
							.orderByAsc("sort_Num"));
			return new RespDataT<List<JfcloudProductType>>(datas);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT<List<JfcloudProductType>>(e);
		}
	}

	@ApiOperation("商品列表")
	@GetMapping(path = "/list")
	@ResponseBody
	public RespDataT<IPage<ProductVo>> list(ProductQueryForm query) {
		try{
			Page<ProductVo> page = new Page<ProductVo>(query.getPage(),query.getPageSize());
			IPage<ProductVo> datas = productMapper.getProductPageList(page,query);
            productService.queryStorageByEntity(datas);
			return new RespDataT<IPage<ProductVo>>(datas);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT<IPage<ProductVo>>(e);
		}
	}


//	@ApiOperation("商品详情")
//	@GetMapping(path = "/detail")
//	@ResponseBody
	public RespDataT<ProductSkuVo> detail(
		@RequestParam(required = true) Long productId) {
		try{
			
			ProductSkuVo datas = productMapper.getProductDetail(productId);
			List<JfcloudProductSku> sku=jfcloudProductSkuService.list(new QueryWrapper<JfcloudProductSku>()
					.eq("pid", productId));
			datas.setProductSkus(sku);
			
			return new RespDataT<ProductSkuVo>(datas);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT<ProductSkuVo>(e);
		}
	}

	@ApiOperation("根据ID批量获取商品信息")
	@GetMapping(path = "/getProductDetails")
	@ResponseBody
	public RespDataT<List<JfcloudProduct>> getProductDetails(
		@RequestParam(required = true) List<Long> ids) {
		try{
			List<JfcloudProduct> datas = (List<JfcloudProduct>) jfcloudProductService.listByIds(ids);
			return new RespDataT<List<JfcloudProduct>>(datas);
		}catch (Exception e) {
			LOGGER.error(e.getMessage(),e);
			return new RespDataT<List<JfcloudProduct>>(e);
		}
	}
	
}
